package bar.dark.lee.fan;

import java.math.BigDecimal;
import java.util.ArrayList;

import bar.dark.lee.BDConstents;
import bar.dark.lee.R;
import bar.dark.lee.SuperActivity;
import bar.dark.lee.common.BDUtil;
import bar.dark.lee.common.DBSQLite;
import bar.dark.lee.common.DBSQLite.ParseSelectData;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;

/**
 * 받을돈
 * @author Administrator
 *
 */
public class FanOutActivity extends SuperActivity
{
	public static final String FAN_OUT_DATA = "fanOutData";
	
	private RadioButton noRadio;
	private RadioButton okRadio;
	private RadioButton lessRadio;

	private DBSQLite sql;
	
	private ListView listView;
	private Button insertButton;
	
	private ArrayList<FanData> fanList;
	
	private View resultView;
	
	private BigDecimal totalMoney;
	private BigDecimal myInMoney;
	private FanData data;
	
	private int queryI;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		sql = DBSQLite.getInstance(this);
		
		setContentView(R.layout.fan_list_activity);
		
		setGoneTitle();
		
		setFindView();
		setOnClickListener();
		
		noRadio.setChecked(true);
	}
	
	@Override
	protected void onResume()
	{
		super.onResume();
		new ShowDialog().execute(0);
	}

	private void setFindView()
	{
		listView = (ListView) findViewById(R.id.fanInListView);
		insertButton = (Button) findViewById(R.id.fanInsertButton);
		resultView = getLayoutInflater().inflate(R.layout.view_no_result, null);
		
		noRadio = (RadioButton) findViewById(R.id.fanNoCheckSign);
		lessRadio = (RadioButton) findViewById(R.id.fanLessCheckSign);
		okRadio = (RadioButton) findViewById(R.id.fanOkCheckSign);
	}
	
	private void setOnClickListener()
	{
		noRadio.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				noRadio.setChecked(true);
				lessRadio.setChecked(false);
				okRadio.setChecked(false);
				
				new ShowDialog().execute(0);
			}
		});
		
		lessRadio.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				noRadio.setChecked(false);
				lessRadio.setChecked(true);
				okRadio.setChecked(false);
				
				new ShowDialog().execute(0);
			}
		});
		
		okRadio.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				noRadio.setChecked(false);
				lessRadio.setChecked(false);
				okRadio.setChecked(true);
				
				new ShowDialog().execute(0);
			}
		});
		
		insertButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(FanOutActivity.this, FanOutInsertActivity.class);
				intent.putExtra(FanOutInsertActivity.INSERT_FLAG, FanOutInsertActivity.INSERT);
				startActivity(intent);
			}
		});
	}
	
	private void setData()
	{
		fanList = new ArrayList<FanData>();
		
		String [] querys = new String[3];
		
		querys[0] = "SELECT * FROM fan_out WHERE checksign = 0 ORDER BY date DESC;";
		querys[1] = "SELECT * FROM fan_out WHERE checksign = 2 ORDER BY date DESC;";
		querys[2] = "SELECT * FROM fan_out WHERE checksign = 1 ORDER BY date DESC;";
		
		myInMoney = BigDecimal.ZERO;
		
		if (noRadio.isChecked())
		{
			queryI = 0;
		}
		else if (lessRadio.isChecked())
		{
			queryI = 1;
		}
		else if (okRadio.isChecked())
		{
			queryI = 2;
		}
		
		sql.selectMapTile(this, querys[queryI], new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				ArrayList<FanData> tempList = new ArrayList<FanData>();
				
				int id = c.getColumnIndex("id");
				int type = c.getColumnIndex("type");
				int name = c.getColumnIndex("name");
				int date = c.getColumnIndex("date");
				int money = c.getColumnIndex("money");
				int tel = c.getColumnIndex("tel");
				int moneyname = c.getColumnIndex("moneyname");
				int checkSign = c.getColumnIndex("checksign");
				int linkId = c.getColumnIndex("linkid");
				int checkId = c.getColumnIndex("checkid");
				int memo = c.getColumnIndex("memo");
				
				c.moveToFirst();
				
				totalMoney = BigDecimal.ZERO;
				myInMoney = BigDecimal.ZERO;
				
				for (int i = 0; i < c.getCount(); i++)
				{
					data = new FanData();
					
					data.setId(c.getInt(id) + "");
					data.setType(c.getInt(type) + "");
					data.setName(c.getString(name));
					data.setDate(c.getInt(date) + "");
					data.setMoney(c.getString(money));
					data.setTel(c.getString(tel));
					data.setMoneyname(c.getString(moneyname));
					data.setCheckSign(c.getInt(checkSign) + "");
					data.setLinkId(c.getInt(linkId) + "");
					data.setCheckId(c.getInt(checkId) + "");
					data.setMemo(c.getString(memo));
					
					totalMoney = totalMoney.add(new BigDecimal(data.getMoney()));
					
					// 받은 돈
					sql.selectMapTile(superContext, "SELECT inmoney FROM in_out WHERE linkid = "
							+ data.getId() + " and category = '"
							+ BDConstents.CATEGORY_FAN_OUT_CHECK + "';", new ParseSelectData()
					{
						@Override
						public void resultData(Cursor c)
						{
							int inmoney = c.getColumnIndex("inmoney");
							
							c.moveToFirst();
							
							for (int i = 0; i < c.getCount(); i++)
							{
								myInMoney = myInMoney.add(new BigDecimal(c.getString(inmoney)));
//								myInMoney += Integer.parseInt(c.getString(inmoney));
								
								BigDecimal lessMoney = new BigDecimal(
										data.getMoney()).subtract(new BigDecimal(c.getString(inmoney)));
								
								data.setMoney(lessMoney.toString());
								
								c.moveToNext();
							}
						}
					});
					
					tempList.add(data);
					
					c.moveToNext();
				}
				
				if (c.getCount() > 0)
				{
					FanData data = new FanData();
					data.setId("-1");
					
					if (queryI == 0)
					{
						data.setCheckSign("0");
						data.setMoney(totalMoney.toString());
						data.setName("빌려준 총합계");
					}
					else if (queryI == 1)
					{
						data.setCheckSign("2");
						data.setMoney(totalMoney.subtract(myInMoney).toString());
						data.setName("남은 총금액");
					}
					else if (queryI == 2)
					{
						data.setCheckSign("1");
						data.setMoney(myInMoney.toString());
						data.setName("받은 총합계");
					}
					
					fanList.add(data);
					fanList.addAll(tempList);
				}
			}
		});
	}
	
	private OnItemClickListener onItemClickListener = new OnItemClickListener()
	{
		@Override
		public void onItemClick(AdapterView<?> arg0, View view, int position,
				long arg3)
		{
			FanData data = fanList.get(position);
			
			if (data.getId().equals("-1"))
			{
				return;
			}
			
			Intent intent = new Intent(FanOutActivity.this, FanOutInfoActivity.class);
			intent.putExtra(FAN_OUT_DATA, data);
			startActivity(intent);
		}
	};
	
	/**
	 * 다이얼로그 뛰우기 
	 */
	private class ShowDialog extends AsyncTask<Integer, Integer, Integer>{
		ProgressDialog dialog = null;
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog = new ProgressDialog(FanOutActivity.this);
			dialog.setCancelable(false);
			dialog.setMessage("Loading...");
			dialog.show();
		}
		
		@Override
		protected Integer doInBackground(Integer... arg0) {
			if (arg0[0] == 0)
			{
				setData();
				return 0;
			}
			
			return null;
		}
		
		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			
			dialog.dismiss();
			
			if (result == 0)
			{
				if (listView.getFooterViewsCount() > 0)
				{
					listView.removeFooterView(resultView);
				}
				
				if (fanList.size() < 1)
				{
					listView.addFooterView(resultView);
					listView.setFooterDividersEnabled(false);
					listView.setOnItemClickListener(null);
				}
				else
				{
					listView.setSelected(true);
					listView.setOnItemClickListener(onItemClickListener);
					listView.setFooterDividersEnabled(true);
				}
				
				FanAdapter adapter = new FanAdapter(FanOutActivity.this, R.layout.list_item_fan, fanList, FanAdapter.OUT);
				listView.setAdapter(adapter);
				adapter.notifyDataSetChanged();
			}
		}
	}

	@Override
	protected void tipButton()
	{
	}
}
